﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using ZM.RedDotSystem;
public class RedDotDataNumBagDemo : MonoBehaviour
{
    public GameObject bagWindow;
    public Button bagButton;
    public Button closeButton;
    public GameObject bagItem;
    public Transform[] itemCellArr;

    bool isCreate = false;
    void Awake()
    {
        for (int i = 0; i < 15; i++)
        {
            RedDotDataMgr.BagDataList.Add(new BagData { id = i, count = i + 1 }) ;
        }
        bagButton.onClick.AddListener(OnBagButtonClick);
        closeButton.onClick.AddListener(OnCloseButtonClick);
        //注册大厅背包数量红点
        RedDotTreeNode bagNode = new RedDotTreeNode { redDotType = RedDotType.RedDotNodeNum, node = RedDotDefine.BagRoot, logicHander = OnRedDotBagLogicHandler };
        RedDotSystem.Instance.InitlizateRedDotTree(new List<RedDotTreeNode> { bagNode });
    }
    public void OnRedDotBagLogicHandler(RedDotTreeNode redNode)
    {
        redNode.redDotCount = 0;
        foreach (var item in RedDotDataMgr.BagDataList)
        {
            if (item.isRead==false)
            {
                redNode.redDotCount++;
            }
        }
        redNode.redDotActive = redNode.redDotCount > 0;
        Debug.Log("redNode.redDotCount:"+ redNode.redDotCount);
    }
  
    public void OnBagButtonClick()
    {
        bagWindow.SetActive(true);
        if (isCreate==false)
        {
            for (int i = 0; i < RedDotDataMgr.BagDataList.Count; i++)
            {
                GameObject obj = Instantiate(bagItem, itemCellArr[i]);
                obj.SetActive(true);
                obj.transform.localPosition = Vector3.zero;
                obj.transform.localScale = Vector3.one;
                obj.transform.rotation = Quaternion.identity;
                BagItem item = obj.GetComponent<BagItem>();
                item.SetData(RedDotDataMgr.BagDataList[i]);
            }
            isCreate = true;
        }
       
    }
    public void OnCloseButtonClick()
    {
        bagWindow.SetActive(false);
    }
}
